package action.auth;

import org.apache.struts2.ServletActionContext;

import service.interf.security.IAuthority;
import service.interf.security.IResource;
import service.interf.security.IResourceAuthority;

import com.opensymphony.xwork2.ModelDriven;

import dao.pojo.ResourceAuthority;
import action.common.MyBaseAction;

public class ResourceAuthAction extends MyBaseAction<ResourceAuthority> implements ModelDriven<ResourceAuthority>{

	private static final long serialVersionUID = 1L;
	private IAuthority iAuth;
	private IResource iRes;
	private IResourceAuthority iResAuth;
	public IAuthority getiAuth() {
		return iAuth;
	}
	public void setiAuth(IAuthority iAuth) {
		this.iAuth = iAuth;
	}
	public IResource getiRes() {
		return iRes;
	}
	public void setiRes(IResource iRes) {
		this.iRes = iRes;
	}
	public IResourceAuthority getiResAuth() {
		return iResAuth;
	}
	public void setiResAuth(IResourceAuthority iResAuth) {
		this.iResAuth = iResAuth;
	}
	
	public ResourceAuthAction() {
		super(ResourceAuthority.class);
	}
	
	public ResourceAuthority getModel(){
		return model;
	}
	
	/********************method*****************************/
	/**
	 * 通过资源获取与资源相关联的权限
	 */
	public void fingByRes(){
		String s = null;
		try{
			s = iResAuth.findByProperty("ResourceAuthority",
					"authority.authority(authority)",
					"json",
					true,
					new String[]{"resource.resourceId"}, 
					new Object[]{model.getResource().getResourceId()}, 
					null,
					null,
					model.getPage(), 
					model.getRows());
		}catch (Exception e) {
			errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), s);
	}
	
	/**
	 * 修改与资源相关联的权限
	 */
	public void updResourceAuth(){
		String s = null;
		try{
			s = iRes.updateResourceAuthority(model.getResource().getResourceId(),model.getIds());
		}catch (Exception e) {
			errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), s);
	}
	
	
	/**
	 * 通过权限获取与权限相关联的资源
	 */
	public void findByAuth(){
		String s = null;
		try{
			s = iResAuth.findByProperty("ResourceAuthority",
					"resource.resourceId(resourceId),resource.url(url)",
					"json",
					true,
					new String[]{"authority.authority"}, 
					new Object[]{model.getAuthority().getAuthority()}, 
					null,
					null,
					model.getPage(), 
					model.getRows());
		}catch (Exception e) {
			errorHandle.handleErr(e);
		}
		servletHandle.writeToClient(ServletActionContext.getResponse(), s);
	}
	
	/**
	 * 修改与资源相关联的权限
	 */
	public void updAuthResource(){
		String s = null;
		try{
			s = iRes.updateAuthorityResource(model.getAuthority().getAuthority(),model.getIds());
		}catch (Exception e) {
			errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), s);
	}
}
